package wikiPhilia_two;

import java.util.ArrayList;
import java.util.HashMap;

import processing.core.PApplet;
import processing.core.PFont;
import processing.core.PImage;
import processing.core.PShape;
import processing.core.PVector;

public class WikiPhilia_two_005  {
	public HashMap<String, WNode> allNodes = new HashMap<String, WNode>();
	public ArrayList<WNode> allNodesList = new ArrayList<WNode>();
	public ArrayList<WNode> nodesLevelZeroList = new ArrayList<WNode>();
	public HashMap<String, WNode> nodesLevelZero = new HashMap<String, WNode>();
	public ArrayList<WNode> nodesLevelOneList = new ArrayList<WNode>();
	public HashMap<String, WNode> nodesLevelOne = new HashMap<String, WNode>();
	public ArrayList<WNode> nodesLevelTwoList = new ArrayList<WNode>();
	public HashMap<String, WNode> nodesLevelTwo = new HashMap<String, WNode>();
	public ArrayList<WEdge> allEdgesList = new ArrayList<WEdge>();
	PApplet p5;
	TsvTableHuge inputTable;
	String personZeroTitle;
	// String inputFileName = "Albert_Einstein_complete_stuff.tsv";
	String inputFileName;
	float minRadiusZero = 100f, maxRadiusZero = 200f, minRadiusOne = 200f,
			maxRadiusOne = 400f;
	static float yAngle;
	static float xAngle;
	int loadSize = 500;
	float interval = 0.1f * 30f; // in 30 fps
	int currentRow = 1;
	int maxLinksCount[] = new int[4];
	int maxBacklinksCount[] = new int[4];
	PFont myFont;
	float xOff, yOff, zOff;
	boolean linear = false;
	//von Pierre
	public static PImage myBg, nebel, planet, network;
	public static PShape svg;
	String dataRoot = "E:\\projects\\eclipse_projects\\wikiphilia\\src\\data\\";
//	private PImage myPlanet; 
//	private PImage network; 
//	private PShape svg; 
	static boolean hideEdges = false;

	
	WikiPhilia_two_005(PApplet parent, String name, String fileName) {
		personZeroTitle = name;
		inputFileName = fileName;
		p5 = parent;
	}

	public void setup() {
		size(800,800,P3D);
		myBg = p5.loadImage (dataRoot + "bg_2000.jpg");
		nebel = p5.loadImage (dataRoot + "nebel.png");
		planet = p5.loadImage(dataRoot + "particle.png"); 
		svg = p5.loadShape(dataRoot + "cat4_black.svg");
		network = p5.loadImage(dataRoot + "network4.png"); 
//		System.out.println("The table " + fileName + " has " + testTable.getRowCount() + " rows.");
//		System.out.println("The table " + fileName + " has " + testTable.data[0].length + " coloumns.");
//		int k = 15000;	
//		myNodes.put(centerArticle.getTitle(),new WNode(this,centerArticle.getTitle()));
//		centerNode = myNodes.get(centerArticle.getTitle());
//		centerNode.setType(WNode.TYPE_PERSON_ZERO);
//		mySelectedNodes.add(centerNode);
//		mySelectedNodesTitles.add(centerNode.getTitle());
//		addNodesEdges(testTable, 0, k);
//		counter = k;
//		myFont = p5.loadFont("data\\TheSerifSemiBold-Plain-24.vlw");
//		p5.textFont(myFont,9);
//		p5.textMode(p5.MODEL);
	
//        float xOff = width/2;
//        float yOff = height/2;
		inputTable = new TsvTableHuge(p5, inputFileName);
		putNode(new WNode(p5, personZeroTitle), 0);
		nodesLevelZeroList.get(0).setType(WNode.TYPE_PERSON_ZERO);
		nodesLevelZeroList.get(0).setPos(new PVector(0, 0, 0));
		System.out.println("The table " + inputFileName + " has "
				+ inputTable.getRowCount() + " rows.");
		// System.out.println("The table " + inputFileName + " has "
		// + inputTable.getRowToStringArray(0).length + " coloumns.");
		p5.noFill();
		p5.noStroke();
		myFont = p5.loadFont("data\\TheSerifSemiBold-Plain-48.vlw");
		p5.textFont(myFont, 9);
		p5.textAlign(p5.CENTER);
		p5.textMode(p5.MODEL);
		xOff = 0;
		yOff = 0;
		for (int i = 0; i < maxLinksCount.length; i++) {
			maxLinksCount[i] = -1;
			maxBacklinksCount[i] = -1;
		}

	}

	public void draw() {
		p5.background(0);
		p5.pushMatrix();
		p5.translate(0,0,-900);
		p5.beginShape();
		p5.texture(myBg); 
		p5.vertex((p5.width-myBg.width)/2, (p5.width-myBg.width)/2, 0,0);
		p5.vertex(p5.width-((p5.width-myBg.width)/2),(p5.width-myBg.width)/2, 0, 0, myBg.width);
		p5.vertex(p5.width-((p5.width-myBg.width)/2),p5.width-((p5.width-myBg.width)/2) , myBg.width, myBg.width);
		p5.vertex((p5.width-myBg.width)/2, p5.width-((p5.width-myBg.width)/2), 0, 0, myBg.width);
		//rect(0,0,400, 400); 
		p5.endShape();
		p5.popMatrix();
		
		
		p5.translate(xOff, yOff, zOff);
		p5.translate(p5.width / 2, p5.height / 2);
		xAngle = p5.map(p5.mouseY, 0, p5.height, 0, p5.TWO_PI);
		yAngle = p5.map(p5.mouseX, 0, p5.width, 0, p5.TWO_PI) + p5.PI;
		p5.rotateX(xAngle);
		p5.rotateY(yAngle);
		p5.beginShape();
		//	textureMode(NORMALIZED);
			//tint(0,20,20,255); 
 

		if (p5.frameCount % interval == 0) {
			addNodesEdges(inputTable, currentRow, currentRow + loadSize - 1);
			currentRow = p5.min((currentRow + loadSize),
					inputTable.getRowCount());
			if (!linear) {
				setSphericalPositions(nodesLevelOneList, nodesLevelZeroList.get(0),
						minRadiusZero, maxRadiusZero, 301, 502);
				setSphericalPositions(nodesLevelTwoList, nodesLevelZeroList.get(0),
						minRadiusOne, maxRadiusOne, 301, 502);	
			} else {
				setLinearPositions(nodesLevelOneList, nodesLevelZeroList.get(0),maxRadiusZero,
						minRadiusZero, maxRadiusZero);
				setLinearPositions(nodesLevelTwoList, nodesLevelZeroList.get(0), maxRadiusOne,
						minRadiusOne, maxRadiusOne);
			}
			
			// setImportanceHighlights(nodesLevelOneList.get(0), 0.5f, 1,
			// "links");
			setImportanceHighlights(0.75f, "backlinks");
			System.out.println("Max Link Count: " + maxLinksCount[3]);
			System.out.println("Max Link Count: " + maxLinksCount[3]);
			System.out.println("Links from l0: " + maxLinksCount[0]);
			System.out.println("Backlinks to l0: " + maxLinksCount[0]);
			System.out.println("Links from l1: " + maxLinksCount[2]);
			System.out.println("Backlinks to l1: " + maxLinksCount[1]);
			System.out.println("Links from l2: " + maxLinksCount[2]);
			System.out.println("Backlinks to l2: " + maxLinksCount[2]);
			System.out.println("Nodes l0: " + nodesLevelZeroList.size());
			System.out.println("Nodes l1: " + nodesLevelOneList.size());
			System.out.println("Nodes l2: " + nodesLevelTwoList.size());
			System.out.println("Nodes altogether: " + allNodesList.size());
			System.out.println("Edges altogether: " + allEdgesList.size());
			System.out.println("Parsing line: " + currentRow + " out of: "
					+ inputTable.getRowCount());

		}
		for (int i = 0; i < allNodesList.size(); i++) {
			allNodesList.get(i).update();
			allNodesList.get(i).display();

		}
		for (int i = 0; i < nodesLevelOneList.size(); i++) {
			nodesLevelOneList.get(i).display();
		}
		for (int i = 0; i < nodesLevelTwoList.size(); i++) {
			nodesLevelTwoList.get(i).display();
		}
		for (int i = 0; i < allEdgesList.size(); i++) {
			allEdgesList.get(i).display();
		}
	}

	public void setSphericalPositions(ArrayList<WNode> theNodes, WNode origin,
			float minR, float maxR, int hC, int vC) {
		int nodeCount = theNodes.size();
		float myInc = 1 / (float) (nodeCount);
		// centerNode.setPos(new PVector(0,0,0));
		float hCycles = hC;
		float vCycles = vC;
		int originIndex;
		if (theNodes.contains(origin)) {
			originIndex = theNodes.indexOf(origin);
		} else {
			originIndex = -1;
		}
		for (int i = 0; i < nodeCount; i++) {
			if (i != originIndex) {
				float myRadius = minR
						- (theNodes.get(i).getLinksCount(3) / (float) maxLinksCount[3])
						* (maxR - minR);
				// float myRadius = maxR; ///just for testing purposes!
				float myIncH = myInc * i;
				float myIncV = myInc * i;
				float lambda = p5.TWO_PI * myInc * i * hCycles;
				float beta = p5.TWO_PI * myInc * i * vCycles;
				float x = origin.getcPos().x + p5.sin(lambda) * p5.cos(beta)
						* myRadius;
				float y = origin.getcPos().y + p5.sin(beta) * myRadius;
				float z = origin.getcPos().z + p5.cos(lambda) * p5.cos(beta)
						* myRadius;
				theNodes.get(i).setPos(new PVector(x, y, z));
			}
		}

	}
	public void setLinearPositions(ArrayList<WNode> theNodes, WNode origin,
			float w, float minH, float maxH) {
		int nodeCount = theNodes.size();
		float myInc = 1 / (float) (nodeCount);
		// centerNode.setPos(new PVector(0,0,0));
		int originIndex;
		if (theNodes.contains(origin)) {
			originIndex = theNodes.indexOf(origin);
		} else {
			originIndex = -1;
		}
		for (int i = 0; i < nodeCount; i++) {
			if (i != originIndex) {
				float myHeight = minH
				- (theNodes.get(i).getLinksCount(3) / (float) maxLinksCount[3])
				* (maxH - minH);
				// float myRadius = maxR; ///just for testing purposes!
				float x = origin.getcPos().x + -w/2+myInc*i*w;
				float y = origin.getcPos().y + myHeight;
				float z = origin.getcPos().z ;
				theNodes.get(i).setPos(new PVector(x, y, z));
			}
		}
		
	}

	public void addNodesEdges(TsvTableHuge sourceTable, int startIndex,
			int endIndex) {
		for (int i = p5.min(sourceTable.getRowCount(), startIndex); i < p5.min(
				sourceTable.getRowCount(), endIndex); i++) {
			String fromTitle = inputTable.getString(i, 0);
			String toTitle = inputTable.getString(i, 1);
			String fromType = inputTable.getString(i, 2);
			String toType = inputTable.getString(i, 3);
			int iFromType = WNode.TYPE_ARTICLE;
			int iToType = WNode.TYPE_ARTICLE;
			if (fromType.equalsIgnoreCase("person"))
				iFromType = WNode.TYPE_PERSON;
			if (fromType.equalsIgnoreCase("non_person"))
				iFromType = WNode.TYPE_NON_PERSON;
			if (fromType.equalsIgnoreCase("category"))
				iFromType = WNode.TYPE_CATEGORY;
			if (toType.equalsIgnoreCase("person"))
				iToType = WNode.TYPE_PERSON;
			if (toType.equalsIgnoreCase("non_person"))
				iToType = WNode.TYPE_NON_PERSON;
			if (toType.equalsIgnoreCase("category"))
				iToType = WNode.TYPE_CATEGORY;
			WNode fromNode = new WNode(p5, fromTitle, iFromType);
			WNode toNode = new WNode(p5, toTitle, iToType);
			// TODO: loops!!!!
			int myCaseFrom = 5;
			int myCaseTo = 5;
			int myCase;// /// is a product of the abovementioned
			if (nodesLevelZero.containsKey(fromTitle))
				myCaseFrom = 1;
			if (nodesLevelOne.containsKey(fromTitle))
				myCaseFrom = 2;
			if (nodesLevelTwo.containsKey(fromTitle))
				myCaseFrom = 3;
			if (nodesLevelZero.containsKey(toTitle))
				myCaseTo = 1;
			if (nodesLevelOne.containsKey(toTitle))
				myCaseTo = 2;
			if (nodesLevelTwo.containsKey(toTitle))
				myCaseTo = 3;
			myCase = myCaseFrom * myCaseTo;
			int type = WEdge.TYPE_UNDEF;
			switch (myCase) {
			case 1: // /nonsense
				System.out.println("ERROR IN ALGORYTHM!");
				break;
			case 2: // / 0 <-> 1 check for loop?
				if (nodesLevelZero.containsKey(fromTitle)) {
					nodesLevelZero.get(fromTitle).incLinks(1);
					nodesLevelOne.get(toTitle).incBacklinks(0);
					if (checkLoop(fromNode, toNode)) {
						type = WEdge.TYPE_LOOP;
					}
					allEdgesList.add(new WEdge(p5, fromNode, toNode, type));
				} else {
					nodesLevelOne.get(fromTitle).incLinks(0);
					nodesLevelZero.get(toTitle).incBacklinks(1);
					if (checkLoop(fromNode, toNode)) {
						type = WEdge.TYPE_LOOP;
						allEdgesList.add(new WEdge(p5, fromNode, toNode, type));
					}
				}
				break;
			case 3: // / 0 <-> 3; nonsense
				System.out.println("ERROR IN ALGORYTHM!");
				break;
			case 4: // / 1 <-> 1: check for loop?
				nodesLevelOne.get(fromTitle).incLinks(1);
				nodesLevelOne.get(toTitle).incBacklinks(1);
				if (checkLoop(fromNode, toNode)) {
					type = WEdge.TYPE_LOOP;
					allEdgesList.add(new WEdge(p5, fromNode, toNode, type));
				}
				break;
			case 5: // / 0 <->NA => 0-1n
				if (nodesLevelZero.containsKey(fromTitle)) {
					putNode(toNode, 1);
					nodesLevelZero.get(fromTitle).incLinks(1);
					toNode.incBacklinks(1);
					allEdgesList.add(new WEdge(p5, nodesLevelZero
							.get(fromTitle), toNode));
					type = WEdge.TYPE_UNDEF;
					if (checkLoop(fromNode, toNode)) {
						type = WEdge.TYPE_LOOP;
						allEdgesList.add(new WEdge(p5, fromNode, toNode, type));
					}
				} else {
					putNode(fromNode, 1);
					nodesLevelZero.get(toTitle).incBacklinks(1);
					toNode.incLinks(1);
					allEdgesList.add(new WEdge(p5, fromNode, nodesLevelZero
							.get(toTitle)));
				}
				break;
			case 6: // / 1 <-> 2: check for loop?
				if (nodesLevelOne.containsKey(fromTitle)) {
					nodesLevelOne.get(fromTitle).incLinks(2);
					nodesLevelTwo.get(toTitle).incBacklinks(1);
					type = WEdge.TYPE_UNDEF;
					if (checkLoop(fromNode, toNode)) {
						type = WEdge.TYPE_LOOP;
						allEdgesList.add(new WEdge(p5, fromNode, toNode, type));
					} else {
						// nodesLevelTwo.get(fromTitle).incLinks(1);
						// nodesLevelOne.get(toTitle).incBacklinks(2);
						allEdgesList.add(new WEdge(p5, fromNode, toNode));
						type = WEdge.TYPE_UNDEF;
						if (checkLoop(fromNode, toNode)) {
							type = WEdge.TYPE_LOOP;
							allEdgesList.add(new WEdge(p5, fromNode, toNode,
									type));
						}
					}
				}
				break;
			case 9: // / 2 <-> 2: check for loop?
				nodesLevelTwo.get(fromTitle).incLinks(2);
				nodesLevelTwo.get(toTitle).incBacklinks(2);
				allEdgesList.add(new WEdge(p5, fromNode, toNode));
				if (checkLoop(fromNode, toNode)) {
					type = WEdge.TYPE_LOOP;
					allEdgesList.add(new WEdge(p5, fromNode, toNode, type));
				}
				break;
			case 10: // / 1 <-> NA => 1-2n
				if (nodesLevelOne.containsKey(fromTitle)) {
					putNode(toNode, 2);
					nodesLevelOne.get(fromTitle).incLinks(2);
					toNode.incBacklinks(1);
					allEdgesList.add(new WEdge(p5,
							nodesLevelOne.get(fromTitle), toNode));
				} else {
					putNode(fromNode, 2);
					nodesLevelOne.get(toTitle).incBacklinks(2);
					toNode.incLinks(1);
					allEdgesList.add(new WEdge(p5, fromNode, nodesLevelOne
							.get(toTitle)));
				}
				break;
			case 15: // / 2 <-> NA => 2-2n
				if (nodesLevelTwo.containsKey(fromTitle)) {
					putNode(toNode, 2);
					nodesLevelTwo.get(fromTitle).incLinks(2);
					toNode.incBacklinks(2);
					allEdgesList.add(new WEdge(p5,
							nodesLevelTwo.get(fromTitle), toNode));
				} else {
					putNode(fromNode, 2);
					nodesLevelTwo.get(toTitle).incBacklinks(2);
					toNode.incLinks(2);
					allEdgesList.add(new WEdge(p5, fromNode, nodesLevelTwo
							.get(toTitle)));
				}
				break;
			case 25: // / NA <-> NA => 2n-2n
				putNode(fromNode, 2);
				putNode(toNode, 2);
				fromNode.incLinks(2);
				toNode.incBacklinks(2);
				allEdgesList.add(new WEdge(p5, fromNode, toNode));
				break;

			default:
				break;
			}
		}
		// ////LOOP handling!!!
		for (int i = 0; i < allNodesList.size(); i++) {
			for (int j = 0; j < 4; j++) {
				if (maxLinksCount[j] < allNodesList.get(i).getLinksCount(j)) {
					maxLinksCount[j] = allNodesList.get(i).getLinksCount(j);
				}
				;
				if (maxBacklinksCount[j] < allNodesList.get(i)
						.getBacklinksCount(j)) {
					maxBacklinksCount[j] = allNodesList.get(i)
							.getBacklinksCount(j);
				}
				;
			}
		}
	}

	public boolean checkLoop(WNode fromNode, WNode toNode) {
		for (int i = 0; i < allEdgesList.size(); i++) {
			if (allEdgesList.get(i).getToNode().equals(fromNode)
					&& allEdgesList.get(i).getFromNode().equals(toNode)) {
				return true;
			}
		}
		return false;
	}

	public void putNode(WNode theNode, int level) {
		switch (level) {
		case 0:
			nodesLevelZero.put(theNode.getTitle(), theNode);
			nodesLevelZeroList.add(theNode);
			allNodes.put(theNode.getTitle(), theNode);
			allNodesList.add(theNode);
			theNode.setLevel(WNode.LEVEL_ZERO);
			break;
		case 1:
			nodesLevelOne.put(theNode.getTitle(), theNode);
			nodesLevelOneList.add(theNode);
			allNodes.put(theNode.getTitle(), theNode);
			allNodesList.add(theNode);
			theNode.setLevel(WNode.LEVEL_ONE);
			break;
		case 2:
			nodesLevelTwo.put(theNode.getTitle(), theNode);
			nodesLevelTwoList.add(theNode);
			allNodes.put(theNode.getTitle(), theNode);
			allNodesList.add(theNode);
			theNode.setLevel(WNode.LEVEL_TWO);
			break;

		default:
			break;
		}
	};

	public void setImportanceHighlights(WNode theNode, float threshold,
			int level, String type) {
		if (type.equalsIgnoreCase("links")) {
			for (int j = 0; j < allEdgesList.size(); j++) {
				if (allEdgesList.get(j).getFromNode().getTitle()
						.equalsIgnoreCase(theNode.getTitle())) {
					if (allEdgesList.get(j).getFromNode().getLinksCount(level) >= maxLinksCount[level]
							* threshold) {
						allEdgesList.get(j).getFromNode()
								.setState(WNode.STATE_HIGHLIGHTED);
						allEdgesList.get(j).setState(WEdge.STATE_HIGHLIGHTED);
					}

					if (allEdgesList.get(j).getToNode().getLinksCount(level) >= maxLinksCount[level]
							* threshold) {
						allEdgesList.get(j).getToNode()
								.setState(WNode.STATE_HIGHLIGHTED);
						allEdgesList.get(j).setState(WEdge.STATE_HIGHLIGHTED);
					}
					if (allEdgesList.get(j).getFromNode().getState() == WNode.STATE_HIGHLIGHTED
							&& allEdgesList.get(j).getToNode().getState() == WNode.STATE_HIGHLIGHTED) {
						allEdgesList.get(j).setState(WEdge.STATE_HIGHLIGHTED);
					}

				} else {
					allEdgesList.get(j).setState(WEdge.STATE_DEFAULT);
					allEdgesList.get(j).getToNode()
							.setState(WNode.STATE_DEFAULT);
					allEdgesList.get(j).getFromNode()
							.setState(WNode.STATE_DEFAULT);
				}

			}
		} else {
			for (int j = 0; j < allEdgesList.size(); j++) {
				if (allEdgesList.get(j).getFromNode().getTitle()
						.equalsIgnoreCase(theNode.getTitle())) {
					if (allEdgesList.get(j).getFromNode()
							.getBacklinksCount(level) >= maxBacklinksCount[level]
							* threshold) {
						allEdgesList.get(j).getFromNode()
								.setState(WNode.STATE_HIGHLIGHTED);
						allEdgesList.get(j).setState(WEdge.STATE_HIGHLIGHTED);
					}

					if (allEdgesList.get(j).getToNode()
							.getBacklinksCount(level) >= maxBacklinksCount[level]
							* threshold) {
						allEdgesList.get(j).getToNode()
								.setState(WNode.STATE_HIGHLIGHTED);
						allEdgesList.get(j).setState(WEdge.STATE_HIGHLIGHTED);
					}
					if (allEdgesList.get(j).getFromNode().getState() == WNode.STATE_HIGHLIGHTED
							&& allEdgesList.get(j).getToNode().getState() == WNode.STATE_HIGHLIGHTED) {
						allEdgesList.get(j).setState(WEdge.STATE_HIGHLIGHTED);
					}

				} else {
					allEdgesList.get(j).setState(WEdge.STATE_DEFAULT);
					allEdgesList.get(j).getToNode()
							.setState(WNode.STATE_DEFAULT);
					allEdgesList.get(j).getFromNode()
							.setState(WNode.STATE_DEFAULT);
				}

			}
		}

	}

	public void setImportanceHighlights(float threshold, int level, String type) {
		if (type.equalsIgnoreCase("links")) {
			for (int j = 0; j < allEdgesList.size(); j++) {
				if (allEdgesList.get(j).getFromNode().getLinksCount(level) >= maxLinksCount[level]
						* threshold) {
					allEdgesList.get(j).getFromNode()
							.setState(WNode.STATE_HIGHLIGHTED);
					allEdgesList.get(j).setState(WEdge.STATE_HIGHLIGHTED);
				} else if (allEdgesList.get(j).getToNode().getLinksCount(level) >= maxLinksCount[level]
						* threshold) {
					allEdgesList.get(j).getToNode()
							.setState(WNode.STATE_HIGHLIGHTED);
					allEdgesList.get(j).setState(WEdge.STATE_HIGHLIGHTED);
				} else {
					allEdgesList.get(j).setState(WEdge.STATE_DEFAULT);
					allEdgesList.get(j).getToNode()
							.setState(WNode.STATE_DEFAULT);
					allEdgesList.get(j).getFromNode()
							.setState(WNode.STATE_DEFAULT);

				}

				if (allEdgesList.get(j).getFromNode().getState() == WNode.STATE_HIGHLIGHTED
						&& allEdgesList.get(j).getToNode().getState() == WNode.STATE_HIGHLIGHTED) {
					allEdgesList.get(j).setState(WEdge.STATE_HIGHLIGHTED);
				}

			}

		} else {
			for (int j = 0; j < allEdgesList.size(); j++) {
				if (allEdgesList.get(j).getFromNode().getBacklinksCount(level) >= maxBacklinksCount[level]
						* threshold) {
					allEdgesList.get(j).getFromNode()
							.setState(WNode.STATE_HIGHLIGHTED);
					allEdgesList.get(j).setState(WEdge.STATE_HIGHLIGHTED);
				}

				else if (allEdgesList.get(j).getToNode()
						.getBacklinksCount(level) >= maxBacklinksCount[level]
						* threshold) {
					allEdgesList.get(j).getToNode()
							.setState(WNode.STATE_HIGHLIGHTED);
					allEdgesList.get(j).setState(WEdge.STATE_HIGHLIGHTED);
				} else {
					allEdgesList.get(j).setState(WEdge.STATE_DEFAULT);
					allEdgesList.get(j).getToNode()
							.setState(WNode.STATE_DEFAULT);
					allEdgesList.get(j).getFromNode()
							.setState(WNode.STATE_DEFAULT);
				}
				if (allEdgesList.get(j).getFromNode().getState() == WNode.STATE_HIGHLIGHTED
						&& allEdgesList.get(j).getToNode().getState() == WNode.STATE_HIGHLIGHTED) {
					allEdgesList.get(j).setState(WEdge.STATE_HIGHLIGHTED);
				}

			}

		}

	}

	public void setImportanceHighlights(float threshold, String type) {
		if (type.equalsIgnoreCase("links")) {
			for (int j = 0; j < allEdgesList.size(); j++) {
				if (allEdgesList.get(j).getFromNode().getLinksCount(3) >= maxLinksCount[3]
						* threshold) {
					allEdgesList.get(j).getFromNode()
							.setState(WNode.STATE_HIGHLIGHTED);
					allEdgesList.get(j).setState(WEdge.STATE_HIGHLIGHTED);
				} else if (allEdgesList.get(j).getToNode().getLinksCount(3) >= maxLinksCount[3]
						* threshold) {
					allEdgesList.get(j).getToNode()
							.setState(WNode.STATE_HIGHLIGHTED);
					allEdgesList.get(j).setState(WEdge.STATE_HIGHLIGHTED);
				} else {
					allEdgesList.get(j).setState(WEdge.STATE_DEFAULT);
					allEdgesList.get(j).getToNode()
							.setState(WNode.STATE_DEFAULT);
					allEdgesList.get(j).getFromNode()
							.setState(WNode.STATE_DEFAULT);

				}

				if (allEdgesList.get(j).getFromNode().getState() == WNode.STATE_HIGHLIGHTED
						&& allEdgesList.get(j).getToNode().getState() == WNode.STATE_HIGHLIGHTED) {
					allEdgesList.get(j).setState(WEdge.STATE_HIGHLIGHTED);
				}

			}

		} else {
			for (int j = 0; j < allEdgesList.size(); j++) {
				if (allEdgesList.get(j).getFromNode().getBacklinksCount(3) >= maxBacklinksCount[3]
						* threshold) {
					allEdgesList.get(j).getFromNode()
							.setState(WNode.STATE_HIGHLIGHTED);
					allEdgesList.get(j).setState(WEdge.STATE_HIGHLIGHTED);
				}

				else if (allEdgesList.get(j).getToNode()
						.getBacklinksCount(3) >= maxBacklinksCount[3]
						* threshold) {
					allEdgesList.get(j).getToNode()
							.setState(WNode.STATE_HIGHLIGHTED);
					allEdgesList.get(j).setState(WEdge.STATE_HIGHLIGHTED);
				} else {
					allEdgesList.get(j).setState(WEdge.STATE_DEFAULT);
					allEdgesList.get(j).getToNode()
							.setState(WNode.STATE_DEFAULT);
					allEdgesList.get(j).getFromNode()
							.setState(WNode.STATE_DEFAULT);
				}
				if (allEdgesList.get(j).getFromNode().getState() == WNode.STATE_HIGHLIGHTED
						&& allEdgesList.get(j).getToNode().getState() == WNode.STATE_HIGHLIGHTED) {
					allEdgesList.get(j).setState(WEdge.STATE_HIGHLIGHTED);
				}

			}

		}

	}

	public void keyPressed() {
		  if (p5.key == 'w') yOff -= 5;
		  if (p5.key == 's') yOff += 5;
		  if (p5.key == 'a') xOff -= 5;
		  if (p5.key == 'd') xOff += 5;
		  if (p5.key == 'e') zOff += 5;
		  if (p5.key == 'c') zOff -= 5;
		  if (p5.key == 'r') {
			  maxRadiusZero += 10;
//				setPositions(200f,100,101);
		  }
		  if (p5.key == 'f') {
			  maxRadiusZero -= 10;
			  if (maxRadiusZero < 0) maxRadiusZero = 0;
//				setPositions(200f,100,101);
		  }
		  
		  if (p5.key == 't') {
			  minRadiusZero += 10;
//				setPositions(200f,100,101);
		  }
		  
		  if (p5.key == 'g') {
			  minRadiusZero -= 10;
			  if (minRadiusZero < 0) minRadiusZero = 0;
//				setPositions(200f,100,101);
		  }
		  
//		  if (p5.key == 'y') {
//		    simpleSetup = !simpleSetup;
//		    setupChanged = true;
//		  }
		  if (p5.key == 'h') {
			  hideEdges = !hideEdges;
		  }
		}
	
}
